package com.hw.one.dam2.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.dam2.param.RegionSiteParam;
import com.hw.one.ext.util.UserUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.util.UserSiteUtil;

@Service
public class RegionSiteService implements IRegionSiteService {

	@Autowired
	private IRegionSiteDao regionSiteDao;
	
	@Override
	public List<Map<String, Object>> findSiteInfoByRegionCode(String regionCode) throws Exception {
		return regionSiteDao.findSiteInfoByRegionCode(regionCode);
	}

	@Override
	public List<Map<String, Object>> findSiteInfoBySiteCode(List<String> regionCodeList) {
		return regionSiteDao.findSiteInfoBySiteCode(regionCodeList);
	}

	@Override
	public List<Map<String, Object>> findSiteInfoByRegionCodeAndSiteType(RegionSiteParam paraMap) throws Exception {
		return regionSiteDao.findSiteInfoByRegionCodeAndSiteType(paraMap);
	}

	@Override
	public Map<String, Object> findRegionInfoById(String regionCode) {
		return regionSiteDao.findRegionInfoById(regionCode);
	}

	@Override
	public List<Map<String, Object>> findParentRegionInfoByRegionCode(String regionCode) throws Exception {
		return regionSiteDao.findParentRegionInfoByRegionCode(regionCode);
	}

	@Override
	public JSONArray findSonRegionInfoByRegionCode(Map<String, Object> paramMap) throws Exception {
		// 根据登录用户，查询用户所属行政区域
		Map<String, Object> region = UserUtil.getRegion();
		String userRegionCode = (String) region.get("regionCode");
		String minLevel = (String) paramMap.get("minLevel");
		List<Map<String, Object>> regionInfoList = regionSiteDao.findSonRegionInfoByRegionCode(userRegionCode);
		JSONArray resultList = new JSONArray();
		for (Map<String, Object> regionInfo : regionInfoList) {
			if (null != minLevel && Integer.valueOf(regionInfo.get("regionLevel").toString()) > Integer.valueOf(minLevel)) {
				continue;
			}
			JSONObject obj = new JSONObject();
			obj.put("regionCode", regionInfo.get("regionCode"));
			obj.put("regionName", regionInfo.get("regionName"));
			resultList.add(obj);
		}
		return resultList;
	}

	
	/**
	 * 根据输入的站点名称内容和给出的站点类型编码，返回地图上已有站点中含有输入内容的站点信息（主要包括经纬度）
	 *  @author  sjd 
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String, Object>> findSiteInfoBySiteTypes(Map<String, Object> map) {
		
		//参数：站点名siteName，站点类型（多个“，”分割）siteTypeList,regionCode
		String siteTypeList = (String)map.get("siteTypeList");
		List<String> siteTypes = UserSiteUtil.convertSiteTypeToCode(siteTypeList);
		System.out.println(siteTypeList.toString());
		map.put("siteTypes", siteTypes);
		map.remove(siteTypeList);
		return regionSiteDao.findSiteInfoBySiteTypes(map);
	}

	/**
	 * 获取当前用户能看到的站点类型的站点数
	 * @param paraMap
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> findSiteNumSiteType(Map<String, Object> map){
		String[] siteTypeCodes = null;
		List<Map<String, Object>> listMap = new ArrayList<>();
		String regionCode = (String) map.get("regionCode");
		Map<String, Object> departmentMap = UserUtil.getDepartment();
        if (null != departmentMap) {
            String siteTypeStr = (String)departmentMap.get("siteType");
            if (null != siteTypeStr) {
            	siteTypeCodes = siteTypeStr.split(",");
                }
        }
        for(String siteTypeCode:siteTypeCodes){
        	Map<String, Object> paramMap = new HashMap<String,Object>();
        	Map<String, Object> resultMap = new HashMap<String,Object>();
        	paramMap.put("siteTypeCode", siteTypeCode);
        	paramMap.put("regionCode", regionCode);
        	List<Map<String, Object>> list = regionSiteDao.findSiteNumSiteType(paramMap);//查出来一个类型的站点的个数
        	resultMap.put(siteTypeCode, list.size());
        	listMap.add(resultMap);
        }
        
	  return listMap;	
	}
	

}
